package com.xboard.server.manager.impl;

import java.util.List;

import org.springframework.stereotype.Component;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;
import com.xboard.server.manager.DataManager;

@Component("dataManager")
public class DataManagerImpl implements DataManager{
	@Override
	public void insert(Entity enty){
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.put(enty);
	}
	@Override
	public List<Entity> list(Query query){
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	    List<Entity> enList = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(10));
	    return enList;
	}
	@Override
	public void update(Entity enty, Query query){
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		PreparedQuery pq = datastore.prepare(query);
		Entity setEnt = pq.asSingleEntity();
		setEnt.setPropertiesFrom(enty);
		datastore.put(setEnt);
	}
	@Override
	public void delete(Query query){
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		PreparedQuery pq = datastore.prepare(query);
		Entity enty = pq.asSingleEntity();
        datastore.delete(enty.getKey());
	}
}
